Page({
  data: {
    content: '',
    images: [],
    contact: '',
    submitting: false
  },

  // 输入反馈内容
  onContentChange(event) {
    this.setData({
      content: event.detail
    });
  },

  // 输入联系方式
  onContactChange(event) {
    this.setData({
      contact: event.detail
    });
  },

  // 上传图片
  async uploadImage() {
    if (this.data.images.length >= 3) {
      wx.showToast({
        title: '最多上传3张图片',
        icon: 'none'
      });
      return;
    }

    try {
      const res = await wx.chooseMedia({
        count: 3 - this.data.images.length,
        mediaType: ['image'],
        sourceType: ['album', 'camera']
      });

      // TODO: 上传图片到服务器
      const images = res.tempFiles.map(file => file.tempFilePath);
      this.setData({
        images: [...this.data.images, ...images]
      });
    } catch (error) {
      console.log('选择图片失败', error);
    }
  },

  // 删除图片
  deleteImage(event) {
    const { index } = event.currentTarget.dataset;
    const images = this.data.images.filter((_, i) => i !== index);
    this.setData({ images });
  },

  // 预览图片
  previewImage(event) {
    const { url } = event.currentTarget.dataset;
    wx.previewImage({
      urls: this.data.images,
      current: url
    });
  },

  // 提交反馈
  async submitFeedback() {
    if (!this.data.content.trim()) {
      wx.showToast({
        title: '请输入反馈内容',
        icon: 'none'
      });
      return;
    }

    this.setData({ submitting: true });

    try {
      // TODO: 调用提交反馈接口
      // await submitFeedback({
      //   content: this.data.content,
      //   images: this.data.images,
      //   contact: this.data.contact
      // });

      wx.showToast({
        title: '提交成功',
        icon: 'success'
      });

      // 延迟返回上一页
      setTimeout(() => {
        wx.navigateBack();
      }, 1500);
    } catch (error) {
      wx.showToast({
        title: '提交失败',
        icon: 'none'
      });
    } finally {
      this.setData({ submitting: false });
    }
  }
}); 